﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Lion.Domain.Contracts;
using Microsoft.Practices.ServiceLocation;
using Forest.Lion.Domain.Services;
using Forest.Lion.Web.Models;
using Forest.Lion.Domain.Shared;
using Forest.Lion.Web.Models.Candidate;
using Forest.Lion.Infrastructure.ModelHelper;
using Forest.Lion.Domain.Models;

namespace Forest.Lion.Web.Controllers
{
    public class CandidateController : AuthorizedController
    {
        #region Const
        private const int _pageSize = 15;
        private const String _dateFormat = "yyyy/MM/dd";
        private const String _defaultKeyword = "请输入职位名称";
        #endregion

        public CandidateController(IUserServices userServices,
             IServiceLocator serviceLocator)
            : base(userServices, serviceLocator)
        {
        }

        public ViewResult List(PagingFilteringModel command,
            CandidateQueryStatus queryStatus = CandidateQueryStatus.All,
            String keyword = "")
        {
            //初始化分页组件
            if (command.PageSize <= 0)
            {
                command.PageSize = _pageSize;
            }
            if (command.PageNumber <= 0)
            {
                command.PageNumber = 1;
            }

            //如果关键字等于默认关键字则置空
            if (keyword.Equals(_defaultKeyword))
            {
                keyword = String.Empty;
            }

            var pagedList = Using<CandidateAppService>().SearchCandidates(this.CurrentUser.Id,
                keyword, queryStatus, command.PageIndex, command.PageSize);

            var model = new CandidateListViewModel()
            {
                PagingFilteringContext = new PagingFilteringModel(),
            };
            model.PagingFilteringContext.LoadPagedList(pagedList);

            DateTime dtNow = DateTime.Now;
            model.CandidateList = pagedList.ToList().DepthCopy<CandidateDomainModel, CandidateItemViewModel>(
                (from, to) =>
                {
                    to.Ages = from.BirthDate.GetArgs();
                    to.LastUpdateDate = from.SaveDate.ToString(_dateFormat);
                    to.RefreshDate = from.RefreshTime.ToString(_dateFormat);
                });

            model.Keyword = keyword;
            model.CandidateQueryStatus = queryStatus;
            ViewBag.DefaultKeyword = _defaultKeyword;
            return View(model);
        }

    }
}
